﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.Entity.Entities;
using Infrastructure.Repositories;

namespace Domain.Services.DomainServices
{
    public class UserDomainService(IUserRepository userRepository, IOrderRepository orderRepository) : IUserDomainService
    {
        public async Task<bool> ChangeUserName(int userId, string newName)
        {
            User user = await userRepository.GetByIdAsync(userId);
            user.UpdateUserName(newName);
            await userRepository.UpdateAsync(user);
            List<Order> order = await orderRepository.GetListAsync(x => x.UserId == user.Id);
            order.ForEach(x => x.UpdateUserName(newName));
            orderRepository.UpdateRange(order);
            return true;
        }
    }
}
